package org.vectrics.recruiter.struts.mail;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.jsp.JspException;

import org.vectrics.SystemException;
import org.vectrics.company.Company;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.location.Address;
import org.vectrics.recruiter.job.Job;
import org.vectrics.struts.UserBufferManager;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;

public class ShowBulkEmailAction extends VectricsStrutsAction {
	private RecruitObjectGraph objectGraph = new RecruitObjectGraph();

	public ShowBulkEmailAction() {
		super();
	}

	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		Person sender = ThreadProperties.getPerson();
		this.objectGraph.addObjectToGraph(sender, "sender");
		
        UserBufferManager bufferManager = new UserBufferManager();
        Collection bufferedObjects = bufferManager.getBufferedObjects(actionData.getRequest());
        log.info("Number of buffered objects: " + bufferedObjects.size());
        Iterator iter = bufferedObjects.iterator();
        while (iter.hasNext()) {
        	Object bufferedObject = iter.next();
        	log.info("Adding object to objectGraph: " + bufferedObject.getClass().getName());
        	if (bufferedObject.getClass().getName().contains("recruiter.job.Job")) {
        		log.info("Object is Job");
        		this.objectGraph.addObjectToGraph(bufferedObject, "job");
        	} else if (bufferedObject instanceof Person) {
        		log.info("Object is person");
        		this.objectGraph.addObjectToGraph(bufferedObject, "person");
        	} else if (bufferedObject.getClass().getName().endsWith("Interview")) {
        		this.objectGraph.addObjectToGraph(bufferedObject, "interview");
        	} else if (bufferedObject instanceof Company) {
        		this.objectGraph.addObjectToGraph(bufferedObject, "company");
        	} else {
        		log.info("Do not know how to handle buffered object of type: " + bufferedObject.getClass().getName());
        	}
        }
		log.info("Attaching object graph to session");
		actionData.getRequest().setSessionAttribute("freemarkerObjectGraph", objectGraph);
		return "success";
	}
	

}
